﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// RouteFilter 的摘要说明
    /// </summary>
    public class RouteFilter : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/plain";
                string key = context.Request["key"];
                string val = context.Request["val"];
                string sortType = context.Request["sortType"];
                string sortField = context.Request["sortField"];

                int pageNo = string.IsNullOrEmpty(context.Request["pageNo"]) ? 1 : int.Parse(context.Request["pageNo"]);

                Route_BLL bll = new Route_BLL();
                //先用假分页  线路反正没有多少
                //string sqllist = "select Top 10    ROUTE_ID,ROUTE_NAME,ROUTE_DES,ROUTE_CHENGDU,ROUTE_BEIJING,ROUTE_ELSECITY,ROUTE_CHARACTERISTIC,ROUTE_DAYS,ROUTE_PRICE,ROUTE_DATE_START,ROUTE_DATE_END,ROUTE_HOT,ROUTE_BAK,ROUTE_ELABORATE,ROUTE_ORIGINAL from (select row_number() over(order by {0} {1}) as rownumber,* from ROUTE {2}) A where rownumber > {3}";

                string sqllist = "select ROUTE_ID,ROUTE_NAME,ROUTE_DES,ROUTE_CHENGDU,ROUTE_BEIJING,ROUTE_ELSECITY,ROUTE_CHARACTERISTIC,ROUTE_DAYS,ROUTE_PRICE,ROUTE_DATE_START,ROUTE_DATE_END,ROUTE_HOT,ROUTE_BAK,ROUTE_ELABORATE,ROUTE_ORIGINAL from (select row_number() over(order by {0} {1}) as rownumber,* from ROUTE {2}) A order by {0} {1}";


                string sqlcount = "select count(*) from ROUTE";
                if (!string.IsNullOrEmpty(key))
                {
                    string filter = string.Empty;
                    if (key == "ROUTE_HOT")
                        filter = string.Format(" where ROUTE_HOT>={0}", int.Parse(val));
                    else if (key == "ROUTE_ELABORATE")
                        filter = string.Format(" where ROUTE_ELABORATE={0}", short.Parse(val));
                    else if (key == "ROUTE_ORIGINAL")
                        filter = string.Format(" where ROUTE_ORIGINAL={0}", short.Parse(val));
                    else if (key == "ROUTE_DAYS")
                        filter = string.Format(" where ROUTE_DAYS>{0}", short.Parse(val));
                    else if (key == "ROUTE_COUNTRY_NAME")
                        filter = string.Format(" WHERE ROUTE_ID IN (SELECT ROUTE_ID FROM ROUTE_COUNTRY WHERE ROUTE_COUNTRY_NAME='{0}')", val);
                    else if (key == "THEME_ID")
                        filter = string.Format(" WHERE ROUTE_ID IN (SELECT ROUTE_ID FROM ROUTE_THEME WHERE THEME_ID='{0}')", val);
                    else if (key == "ROUTE_NAME")
                        filter = string.Format(" where ROUTE_NAME like '%{0}%'", val);
                    else
                        filter = "";

                    sqlcount += filter;
                    sqllist = string.Format(sqllist, sortField, sortType, filter, (pageNo - 1) * 10);
                }
                else
                {
                    sqllist = string.Format(sqllist, sortField, sortType, "", (pageNo - 1) * 10);
                }

                List<ROUTE> routes = bll.GetModels(sqllist);
                context.Response.Write(JsonConvert.SerializeObject(routes));
            }
            catch (Exception ex)
            {
                RESP_Info info = new RESP_Info()
                {
                    Success = false,
                    Tip = "获取路线失败",
                    Title = "错误",
                    Url = ""
                };

                context.Response.Write(JsonConvert.SerializeObject(info));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}